﻿using Sys.Common;
using Sys.Core.Tour;
using Sys.Model;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;

namespace Sys.Api.Tour
{
    public class RouteApi : BaseApi
    {
        public string Grid(int rows, int page, string search, string sort, string order)
        {
            var entities = RouteBll.I.Entities;

            if (!string.IsNullOrEmpty(search))
            {
                entities = entities.Where(m => m.Name.Contains(search));
            }

            var list = RouteBll.I.EntitiesPage(entities, rows, page, sort, order);

            var select = list.Select(m => new
            {
                ID = m.ID,
                Name = m.Name,
                Price = m.Price,
                Child = m.Child,
                Img = m.Img,
                Departure = m.Departure,
                City = m.City,
                Top = m.Top,
                InsertTime = m.InsertTime,
                UpdateTime = m.UpdateTime
            });

            return GetGrid(entities.Count(), select.ToList());
        }

        public string Save(TourRoute model)
        {
            JsonMsg result = new JsonMsg();

            if (string.IsNullOrEmpty(model.ID))
            {
                result.Message = "添加成功!";

                model.ID = StringHelper.GetGuid();

                model.InsertTime = DateTime.Now;
                model.UpdateTime = DateTime.Now;

                result.Success = RouteBll.I.Insert(model) > 0;
            }
            else
            {
                result.Message = "修改成功!";

                model.UpdateTime = DateTime.Now;

                result.Success = RouteBll.I.Update(model) > 0;
            }

            if (!result.Success) { result.Message = "操作失败!"; }

            return JsonHelper.ToJson(result);
        }

        public string Top(string id)
        {
            JsonMsg result = new JsonMsg();

            TourRoute model = RouteBll.I.GetByKey(id);

            if (model != null)
            {
                model.Top = !model.Top;

                result.Success = RouteBll.I.Update(model) > 0;

                if (result.Success)
                {
                    if (model.Top)
                    {
                        result.Message = "顶置成功!";
                    }
                    else
                    {
                        result.Message = "撤销成功!";
                    }
                }
            }

            return JsonHelper.ToJson(result);
        }

        public string Delete(string id)
        {
            JsonMsg result = new JsonMsg();

            result.Success = RouteBll.I.Delete(id) > 0;

            if (result.Success) { result.Message = "删除成功!"; }

            return JsonHelper.ToJson(result);
        }

        public string AddImg()
        {
            Hashtable hash = KindHelper.ImgUpload();

            if (Convert.ToInt32(hash["error"]) == 0)
            {
                HttpContext context = HttpContext.Current;

                string filename = hash["url"].ToString();

                string temppath = context.Server.MapPath("~/userfiles/temp/" + filename);
                string filepath = context.Server.MapPath("~/userfiles/route/big/");
                string defpath = context.Server.MapPath("~/userfiles/route/default/");
                string homepath = context.Server.MapPath("~/userfiles/route/home/");

                if (!Directory.Exists(filepath)) { Directory.CreateDirectory(filepath); }
                if (!Directory.Exists(defpath)) { Directory.CreateDirectory(defpath); }
                if (!Directory.Exists(homepath)) { Directory.CreateDirectory(homepath); }

                ThumNail.MakeThumNail(temppath, filepath + filename, 800, 480, TnType.Cut);
                ThumNail.MakeThumNail(temppath, defpath + filename, 360, 216, TnType.Cut);
                ThumNail.MakeThumNail(temppath, homepath + filename, 246, 178, TnType.Cut);

                System.IO.File.Delete(temppath);

                hash["url"] = filename;
            }

            return JsonHelper.ToJson(hash);
        }

        public string ScenicGrid(string id)
        {
            var entities = RouteScenicBll.I.SortEntities.Where(m => m.RouteID.Equals(id));

            var select = entities.Select(m => new
            {
                ID = m.ID,
                RouteID = m.RouteID,
                ScenicID = m.TourScenic.ID,
                Name = m.TourScenic.Name,
                Img = m.TourScenic.Img,
                IntSort = m.IntSort,
                UpdateTime = m.TourScenic.UpdateTime
            });

            return JsonHelper.ToJson(select.ToList());
        }

        public string InsertScenic(string rid, string sid)
        {
            JsonMsg result = new JsonMsg();

            TourRouteScenic model = new TourRouteScenic();

            model.ID = StringHelper.GetGuid();

            model.RouteID = rid;
            model.ScenicID = sid;

            var entities = RouteScenicBll.I.Entities.Where(m => m.RouteID.Equals(rid));
            model.IntSort = RouteScenicBll.I.MaxSort(entities, m => m.IntSort);

            result.Success = RouteScenicBll.I.Insert(model) > 0;

            if (result.Success) { result.Message = "添加成功!"; }

            return JsonHelper.ToJson(result);
        }

        public string DeleteScenic(string id)
        {
            JsonMsg result = new JsonMsg();

            result.Success = RouteScenicBll.I.Delete(m => m.ID.Equals(id)) > 0;

            if (result.Success) { result.Message = "删除成功!"; }

            return JsonHelper.ToJson(result);
        }

        public string ScenicSetOrder(string id, string rid, string order)
        {
            JsonMsg result = new JsonMsg();

            var entities = RouteScenicBll.I.Entities.Where(m => m.RouteID.Equals(rid));

            bool success = false;

            result.Message = RouteScenicBll.I.SetOrder(entities, id, "IntSort", order, out success);

            result.Success = success;

            return JsonHelper.ToJson(result);
        }
    }
}
